import { get, post } from '../utils/request'

// 获取最新帖子
export const getPostList = ({ queryKey, pageParam }) => {
  const [, filter] = queryKey
  // type 0全部，1图文，2视频
  return get('/post/lastPostByFilter', { page: pageParam, filter, type: '0', limit: 20 })
}

// 获取搜索帖子
export const getSearchPostList = (params, activeIndex) => {
  if (activeIndex == 0) {
    return get('/post/search', params)
  } else if (activeIndex == 1) {
    return get('/user/search', params)
  } else {
    return get('/topic/search', params)
  }
}

export const getSearchUserList = (params) => {
  return get('/user/search', params)
}

export const getSearchTopicList = (params) => {
  return get('/topic/search', params)
}

// 获取热门帖子 
export const getHotPostList = ({ queryKey }) => {
  const [, postId] = queryKey
  console.log(postId)
  return get('/post/hotPost', { page: 1, limit: 10, topicIds: postId })
}

// 获取我加入的主题的帖子
export const getJoinTopicPostList = (params) => {
  return get('/post/joinTopicPost', params)
}

// 获取我关注的用户的帖子
export const getFollowUserPostList = ({ pageParam = 1 }) => {
  return get('/post/followUserPost', { page: pageParam })
}

const sortDiscussFilterMap = {
  0: 'last',
  1: 'hot',
}
// 获取话题帖子
export const getPostByDiscussId = ({ queryKey, pageParam = 1 }) => {
  const [, id, sortFilter] = queryKey
  return post('/post/getPostListByDiscussId', { disId: id, page: pageParam, sort: sortDiscussFilterMap[sortFilter] })
}

const sortFilterMap = {
  0: 'id desc',
  1: 'read_count desc',
}
// 获取主题帖子
export const getPostByTopic = ({ queryKey, pageParam = 1 }) => {
  const [, id, sortFilter] = queryKey
  return post('/post/getListByTopicId', { topicId: id, page: pageParam, order: sortFilterMap[sortFilter] })
}

// 获取帖子详情
export const getPostDetail = ({ queryKey }) => {
  const [, id] = queryKey
  console.log('调用了 get /post/detail', id)
  console.trace('get /post/detail 调用栈')

  // export const getPostDetail = (id) => {
  //   export const getPostDetail = ({ queryKey }) => {
  //     const [, id] = queryKey
  //     console.log('调用了 get /post/detail', id)
  //     console.trace('get /post/detail 调用栈')

  return get('/post/detail', { id })
}

// 获取帖子评论列表
export const getPostCommentList = ({ queryKey, pageParam = 1 }) => {
  const [, postId] = queryKey
  return get('/comment/list', { postId: postId, page: pageParam })
}

// 帖子点赞
export const postAddCollection = (params) => {
  return post('/post/addCollection', params)
}

// 帖子取消点赞
export const postCancelCollection = (params) => {
  return post('/post/cancelCollection', params)
}

// 评论点赞
export const postAddThumbs = (params) => {
  return post('/comment/thumbs', params)
}

// 评论取消点赞
export const postCancelThumbs = (params) => {
  return post('/comment/cancelThumbs', params)
}

// 添加评论
export const postAddComment = (params) => {
  return post('/post/addComment', params)
}

// 添加帖子
export const postAddPost = async (params) => {
  return post('/post/addPost', params)
}

// 删除帖子
export const postDelPost = (params) => {
  return post('/post/del', params)
}

// 获取用户帖子
export const getPostListByUid = async ({ queryKey, pageParam = 1 }) => {
  const [, id] = queryKey
  return post('/post/getListByUid', { uid: id, page: pageParam })
}

// 获取用户帖子
export const getReportPostList = async ({ queryKey, pageParam = 1 }) => {
  const [, status] = queryKey
  return get('/report/list', { status: status, page: pageParam })
}

// 删除评论
export const deleteComment = (id) => {
  return post('/comment/del', { id })
}

// 发布长文
export const postAddArticle = (params) => {
  return post('/post/addArticle', params)
}

// 编辑长文
export const postEditArticle = (params) => {
  return post('/post/updatePost', params)
}

// 获取剩余子评论
export const getRemainComment = ({ queryKey }) => {
  const [, commentId] = queryKey
  return get('/comment/remainComment', { id: commentId })
}
// 添加投票
export const postAddVote = (params) => {
  return post('/post/voteAdd', params)
}
// 编辑加投票
export const postUpdateVote = (params) => {
  return post('/post/updateVote', params)
}


// 投票
export const postVote = (params) => {
  return post('/post/vote/userVote', params)
}


// 获取用户关注的帖子
export const getFollowPostList = async ({ queryKey, pageParam = 1 }) => {
  return get('/post/getFollowPostList', { page: pageParam })
}

//撤回举报
export const reportCancel =  (params) => {
  return get('/report/cancel', params)
}